﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class nhapphong : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("phong");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); 

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public nhapphong()
        {
            InitializeComponent();
            
        }

        private void nhapphong_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            

        }

        private Boolean kiemtra()
        {
            kt = true;

            if (txtsophong.Text == "")
            {
                MessageBox.Show("số phòng không đươc để trống !", "Thông báo", MessageBoxButtons.OK);
                kt = false;
                txtsophong.Focus();
            }
            else if (txtsotang.Text == "")
            {
                MessageBox.Show("Số tầng không được để trống!", "thong bao", MessageBoxButtons.OK);
                kt = false;
                txtsotang.Focus();
            }
            return kt;
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  * from phong ";
            da.SelectCommand = command;
            da.Fill(dt);
        }

        private void btnok_Click(object sender, EventArgs e)
        {
           kiemtra();
            if (kt == true)
            {
                DataRow row = dt.NewRow();
                row["pid"] = txtsophong.Text;
                row["ptang"] = txtsotang.Text;
                row["pdientich"] = txtdientich.Text;
                row["phuongnha"] = txthuongnha.Text;
                row["pthongtin"] = txtthongtin.Text;
                dt.Rows.Add(row);
                SqlCommand commandInsert = new SqlCommand();
                commandInsert.Connection = conn;
                commandInsert.CommandType = CommandType.Text;
                commandInsert.CommandText = @"INSERT INTO phong(pid,ptang,pdientich,phuongnha,pthongtin) VALUES(@pid,@ptang,@pdientich,@phuongnha,@pthongtin) ";

                commandInsert.Parameters.Add("@pid", SqlDbType.Int, 20, "pid");
                commandInsert.Parameters.Add("@ptang", SqlDbType.Int, 20, "ptang");
                commandInsert.Parameters.Add("@pdientich", SqlDbType.NVarChar, 30, "pdientich");
                commandInsert.Parameters.Add("@phuongnha", SqlDbType.NVarChar, 30, "phuongnha");
                commandInsert.Parameters.Add("@pthongtin", SqlDbType.NVarChar, 50, "pthongtin");
                da.InsertCommand = commandInsert;
                da.Update(dt);
                MessageBox.Show("Bạn đã nhập thành công", "thông báo", MessageBoxButtons.OK);
                quanlyphong qlp = new quanlyphong();
                qlp.Show();
                Close();
                Dispose();
                disconnect();
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
            disconnect();
            quanlyphong qlp = new quanlyphong();
            qlp.Show();
        }

                
    }
}
